package com.zhouqinuan.spring.cloud.develop.framework.demo.routing.datasource.service;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zhouqinuan.spring.cloud.develop.framework.base.util.BeanUtil;
import com.zhouqinuan.spring.cloud.develop.framework.demo.routing.datasource.mapper.UserInfoMapper;
import com.zhouqinuan.spring.cloud.develop.framework.demo.routing.datasource.model.UserInfo;
import com.zhouqinuan.spring.cloud.develop.framework.demo.routing.datasource.req.AddUserReq;
import com.zhouqinuan.spring.cloud.develop.framework.demo.routing.datasource.req.GetUserInfoByIdReq;
import com.zhouqinuan.spring.cloud.develop.framework.demo.routing.datasource.req.UpdateUserReq;
import com.zhouqinuan.spring.cloud.develop.framework.demo.routing.datasource.resp.GetUserInfoByIdResp;
import com.zhouqinuan.spring.cloud.develop.framework.routing.datasource.annotation.DataSource;

@Service
public class UserService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @DataSource("demo0_slave")
    public GetUserInfoByIdResp getUserInfoById(GetUserInfoByIdReq reqVO) {

        UserInfo userInfo = userInfoMapper.selectByPrimaryKey(reqVO.getUid());

        return BeanUtil.mapToObject(userInfo, GetUserInfoByIdResp.class);
    }

    public int addUser(AddUserReq reqVO) {
        UserInfo userInfo = BeanUtil.mapToObject(reqVO, UserInfo.class);
        userInfo.setStatus(1);
        userInfo.setCreateTime(new Date());
        return userInfoMapper.insertSelective(userInfo);
    }

    public int updateUser(UpdateUserReq reqVO) {
        UserInfo userInfo = BeanUtil.mapToObject(reqVO, UserInfo.class);
        userInfo.setUpdateTime(new Date());
        return userInfoMapper.updateByPrimaryKeySelective(userInfo);
    }
}
